package cn.mogugo.joke.tld;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import org.hibernate.util.StringHelper;

import cn.mogugo.joke.model.JokeObj;

public class JokeObjNewItemTld  extends BodyTagSupport {
	/**
	 * 开始编号
	 */
	private Integer startNumber;
	/**
	 * 结束编号
	 */
	private Integer endNumber;
	
	private Integer titleLength;
	
	public int doEndTag() throws JspException {
		JokeObjNewTld jokeObjNewTld = (JokeObjNewTld) TagSupport.findAncestorWithClass(this, JokeObjNewTld.class);
		List listObjNew = jokeObjNewTld.listObjNew;
		
		for(int i = startNumber ; i < endNumber ; i ++ ){
			JokeObj jo = (JokeObj)listObjNew.get(i);
			BodyContent bc = getBodyContent();
			String body = bc.getString();
			
			String jokeTitle = jo.getJoke_title();
			if(titleLength != null && titleLength > 0 && jokeTitle.length() > titleLength){
				jokeTitle = jokeTitle.substring(0, titleLength)+"..";
			}
			body = StringHelper.replace(body, "$_jo_joke_title" , jokeTitle);
			body = StringHelper.replace(body, "$_jo_full_joke_title" , jo.getJoke_title());
			body = StringHelper.replace(body, "$_jo_type" , "new");
			body = StringHelper.replace(body, "$_jo_joke_id" , jo.getId().toString());
	        try {
	            pageContext.getOut().print(body);
	        } catch (IOException e) {
	        }
		}

		
		return this.EVAL_PAGE;
				
		
	}

	public Integer getTitleLength() {
		return titleLength;
	}
	public void setTitleLength(Integer titleLength) {
		this.titleLength = titleLength;
	}
	public Integer getStartNumber() {
		return startNumber;
	}
	public void setStartNumber(Integer startNumber) {
		this.startNumber = startNumber;
	}
	public Integer getEndNumber() {
		return endNumber;
	}
	public void setEndNumber(Integer endNumber) {
		this.endNumber = endNumber;
	}
	
	
	
}
